package com.seata;

import com.google.common.base.Charsets;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.exception.ZkMarshallingError;
import org.I0Itec.zkclient.serialize.ZkSerializer;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryUntilElapsed;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 蚂蚁会花呗
 * @date 2021/9/8 11:03
 */
public class ZkDataInit {

    /**
     * 节点路径
     */
    private static final String NODE_PATH = "/seata";

    /**
     * zk 服务地址
     */
    private static final String CONNECT_TOSTRING = "127.0.0.1:2181";
    public static ExecutorService executorService = Executors.newCachedThreadPool();

    public static void main(String[] args) throws Exception {
        try {
            RetryPolicy retryPolicy2 = new RetryUntilElapsed(5000, 1000);
            CuratorFramework curatorFramework = CuratorFrameworkFactory.builder()
                    .connectString(CONNECT_TOSTRING)
                    .sessionTimeoutMs(5000)
                    .connectionTimeoutMs(5000)
                    .retryPolicy(retryPolicy2)
                    .build();
            curatorFramework.start();
            Stat stat1 = curatorFramework.checkExists().forPath(NODE_PATH);
            if(Objects.isNull(stat1)){
                curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(NODE_PATH, null);
            }
            Properties properties = new Properties();
            try {
                //config.txt的绝对路径
                File file=new File("D:/Program Files/seata-1.4.2/script/config-center/config.txt");
                InputStream in = new FileInputStream(file);
                properties.load(in);
                Set<Object> keys = properties.keySet();
                for (Object key : keys) {
                    putConfig(curatorFramework,key.toString(), properties.get(key).toString());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }
    }

    /**
     *
     * @param curatorFramework
     * @param dataId
     * @param content
     * @return
     */
    public static boolean putConfig(CuratorFramework curatorFramework, final String dataId, final String content) {
        Boolean flag = false;
        String path = "/seata/" + dataId;
        try {
            Stat stat1 = curatorFramework.checkExists().forPath(path);
            if (Objects.isNull(stat1)) {
                curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path, content.getBytes());
                flag = true;
            }
            return flag;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;

    }

}
